<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<style>
.table-bordered,.table-bordered>thead>tr>th, .table-bordered>tbody>tr>th, .table-bordered>tfoot>tr>th, .table-bordered>thead>tr>td, .table-bordered>tbody>tr>td, .table-bordered>tfoot>tr>td{
    border: 1px solid #cdcdcd;
}
</style>
<div class="row">
    <div class="col-sm-12">
        <div class="box">
            <div class="box-header with-border">
                <h2 class="page-header">
                    <i class="fa fa-globe"></i> 所有贝宝订单
                </h2>
            </div>
            <div class="box-body">
                <div class="mb-10">
                    <?php echo form_open(current_url(), array('class' => 'form-inline', 'method'=>'get', 'id' => 'search_goods')); ?>                                     
                    <div class="form-group" style="margin-right:15px;">
                    <select class="form-control" name="siteid" id="siteid">
                        <option value="0">所有站点</option>
                        <?php
                        foreach($sindex as $k=>$site)
                        {
                            echo '<option value="'.$k.'">'.$site.'</option>';
                        }
                        ?>
                    </select>
                    </div>
                    <div class="form-group" style="margin-right:15px;">
                    <select class="form-control" name="state">
                        <option value="0">所有状态</option>
                        <?php foreach($status as $k=>$s):?>
                        <option value="<?php echo $k;?>" <?php if($k==$state) echo " selected";?>><?php echo $s;?></option>                                        
                        <?php endforeach;?>
                    </select>
                    </div>
                    <div class="form-group" style="margin-right:15px;">
                    <?php echo form_input($keyword);?>
                    </div>
                    <div class="form-group" style="margin-right:15px;">
                    <?php echo tpl_form_field_daterange("timelimit",["start"=>date("Y-m-d",strtotime("-800 days")),"end"=>date("Y-m-d")],false);?>
                    </div>
                    <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-sm btn-primary btn-flat', 'content' => "搜索订单")); ?>
                    <?php echo form_close();?>
                </div>
                <?php if($loglist):?>
                <table class="table table-bordered table-hover" style="margin-top:20px;">
                    <thead>
                        <tr>
                            <th width="50">ID</th>
                            <th>Pyapal账号</th> 
                            <th>网站</th>
                            <th>订单ID</th>
                            <th width="300">产品信息</th>
                            <th>收件人信息</th>
                            <th>总金额</th>
                            <th>状态</th>
                            <th>返回状态</th>
                            <th>添加时间</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($loglist as $item):?>
                        <tr shopid="<?php echo $item["id"]; ?>">
                            <td><?php echo $item["id"]; ?></td>
                            <td><?php echo $paypalmap[$item["accid"]]["account"];?></td>
                            <td><?php echo $sindex[$item["siteid"]];?></td>
                            <td><?php echo $item["orderid"]; ?></td>
                            <td>
                            <?php
                            $productinfo=json_decode($item["products"],true);
                            if($productinfo):
                                $pcount=0;
                                foreach($productinfo as $pro):
                                    $pcount++;
                            ?>
                            <div class="protitle">
                            <?php echo $pro["name"];?>  x  <small class="label <?php if($pro["quantity"]>1) echo 'bg-red';else echo 'bg-gray';?>" style="border-radius:50%;"><?php echo $pro["quantity"];?></small>
                            </div>
                            <div class="proinfo">
                                USD <?php echo sprintf("%.2f", $pro["price"]);?>
                                <?php
                                if(isset($pro["attr"]))
                                {
                                    echo '<br/>Variant:'.$pro["attr"];
                                }
                                ?>  
                            </div>        
                            <?php 
                            if($pcount<count($productinfo))
                            {
                                echo '<hr class="simple" color="#6f5499" />	';
                            }
                            endforeach;
                            endif;
                            ?>
                            </td>
                            <td>
                            <?php echo $item["realname"];?><br/>
                            <?php echo $item["address"];?><br/>
                            <?php echo $item["city"].' '.$item["state"].' '.$item["postcode"];?><br/>
                            <?php echo $item["country"];?><br/> 
                            <?php echo $item["phone"];?>
                            </td>
                            <td><?php echo $item["curreny"].' '.$item["amount"]; ?> </td>
                            <td><span class="label <?php echo $statuslabel[$item["status"]]; ?>"><?php echo $status[$item["status"]];?></span></td>
                            <td><?php echo $item["ipnstatus"]; ?> </td>
                            <td><?php echo $item["addtime"]; ?></td>
                        </tr>
                        <?php endforeach;?>
                    </tbody>
                </table>
                <?php endif;?>
                <?php 
                if(empty($loglist)){
                    echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
                }
                ?>
                <?php echo $pager;?>
                <div class=" pull-right form-inline" style="line-height:30px;margin-top:25px;">
                    <?php echo '共'.$total.'条订单，'.$page.'/'.$pages.'页';?>
                    <select id="pagesize" class="form-control input-sm" style="height:22px;padding:0;">
                        <option value="0">每页数量</option>
                        <option value="50">50</option>
                        <option value="100">100</option>
                        <option value="200">200</option>
                        <option value="500">500</option>
                    </select>
                    <input type="text" id="topage" value="" class="form-control input-sm" placeholder='页码' style="margin:0 5px;height:22px;width:45px;"> 
                    <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-xs btn-primary btn-flat', 'id'=>'jumpbtn','content' => "跳转")); ?>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
$(document).ready(function() {
    $("#siteid").select2();
    $("select#pagesize").change(function(){
        pagesize=$("select#pagesize option:selected").val();
        if(pagesize>0)
        {
            var url= window.location.href;
            var newUrl=  changeURLArg(url, "pagesize", pagesize); 
            window.location.href =  newUrl;
        }
    });
    function changeURLArg(url, arg, arg_val) {
        var pattern = arg + '=([^&]*)';
        var replaceText = arg + '=' + arg_val;
        if (url.match(pattern)) {
            var tmp = '/(' + arg + '=)([^&]*)/gi';
            tmp = url.replace(eval(tmp), replaceText);
            return tmp;
        } else {
            if (url.match('[\?]')) {
                return url + '&' + replaceText;
            } else {
                return url + '?' + replaceText;
            }
        }
        return url + '\n' + arg + '\n' + arg_val;
    }
});
</script>
